package de.szut.splasher;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Picture;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.PictureDrawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;

/**
 * Just a SVG - TEST.
 * TODO: refactor
 */
public class MainActivity extends Activity implements OnTouchListener {

    /**
     * Image view for the displayed map
     */
    private ImageView map;

    /**
     * Image view for the colored map in the background (not visible)
     */
    private ImageView backgroundCoords;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//        SVG svgMap = SVGParser.getSVGFromResource(getResources(), R.raw.calculateMapsPixel);
//        SVG svgCoords = SVGParser.getSVGFromResource(getResources(),
//                R.raw.test_colored);
//        this.map = (ImageView) this.findViewById(R.id.map);
//        this.backgroundCoords = (ImageView) this.findViewById(R.id.coord_image);
//        this.map.setImageDrawable(svgMap.createPictureDrawable());
//        this.map.setOnTouchListener(this);
//        this.backgroundCoords.setImageDrawable(this.createBitmapDrawable(
//                this.getApplicationContext(), svgCoords.getPicture()));
    }

    public BitmapDrawable createBitmapDrawable(Context context, Picture picture) {
        Bitmap bitmap = Bitmap.createBitmap(picture.getWidth(),
                picture.getHeight(), Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        PictureDrawable drawable = new PictureDrawable(picture);
        drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
        drawable.draw(canvas);
        return new BitmapDrawable(context.getResources(), bitmap);
    }

    public Bitmap getBitmapFromView(ImageView view) {
        Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(),
                view.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(returnedBitmap);
        Drawable bgDrawable = view.getDrawable();
        if (bgDrawable != null) {
            bgDrawable.draw(canvas);
        }
        view.draw(canvas);
        return returnedBitmap;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onTouch(View view, MotionEvent event) {
        float x = event.getX();
        float y = event.getY();
        Bitmap coorBitmap = this.getBitmapFromView(this.backgroundCoords);
        int color = coorBitmap.getPixel((int) x, (int) y);
        System.out.println(color);
        return false;
    }
}
